clear all
clc
global xt
global yt
global y1
global iteracion
iteracion=0;
estacion='soleado'
global prueba_RMSE_val
global prueba_RMSE_test
global prueba_RMSE_entrenamiento

entrada_date=[];
entrada_temp=[];
entrada_rad=[];
entrada_humidity=[];
entrada_speed=[];
entrada_gust=[];
entrada_UV=[];
salida=[];
input=[];
x=[0.689222982	5.103791106	-5.375542197	-0.151964599	0.143685784	-6.595564347	-6.184988895	-0.224086239	-0.055466259	-6.608479883	-0.199480991	0.973460244	0.692485303	8.696590691	0.042131616	3.727861984	-3.758541782	-9.694872787	6.290316982	7.944658584	-7.603567602	-0.298528499	-3.372235618	-8.886177988	-4.416031908	5.706992941	-6.744648454	2.840011747	7.558495688	8.687737669	-2.53426275	2.736965095	1.829474623	8.663355481	9.807528571	0.47573532	-8.741483325	-2.503708029	-3.738168891	6.063772202	1.287433328	3.998950381	-8.431806014	6.006973998	2.277608486	7.213931231	-3.491208196	-9.492590043	-0.775226694	2.893445929	7.443254404	6.114685462	1.048465937	-7.244697033	4.199340669	5.714362706	-3.755936743	-7.07367182	3.029581038	8.20378633	-1.986117583	3.656305797	6.122606859	-2.16054069	2.79932732	-2.174086359	9.030050601	3.678692103	3.174861627	-4.134759829	6.756997829	8.346880259	-3.915138339	6.862236649	-0.9505992	-1.563496734	-9.73256612	1.770728403	0.875669357	-7.549905161	-1.078269818	2.053556101	-4.710890631	5.75197307	7.983989342	-0.947907657	5.032626726	-9.857331124	9.106757812	-3.104964616	-9.980432236	-8.438347018	-4.673676874	2.831941	-3.694550017	5.081098319	4.542424685	-2.253404094	4.523291833	3.204537509	-7.529121679	6.593521474	-7.808776836	8.782090457	-1.244576646	-2.24229024	-9.987770596	-8.774951199	8.327436663	-0.489648853	4.675476573	8.589974582	0.145668454	7.329843155	-2.609592086	-0.893034176	4.947944805	-1.061693772	0.584668564	-9.925198165	-6.682560218	2.78589725	-6.940997823	2.279623809	9.212475986	-2.444512029	-6.836122365	8.578327863	-2.495518245	-7.395454941	-5.659615104	-9.899406811	6.236896083	4.452176892	-9.276452292	-0.606412052	-4.692044918	2.569678764	7.345898346	-9.031057476	-9.637634319	-7.269615937	7.136240149	9.952562644	3.077272028	9.279276911	-2.113046318	5.966968049	-7.27211259	-0.041020647	-5.97287486	7.276612371	-7.877443926	-4.659711096	4.904904316	5.98591175	0.973608692	5.045618533	-5.050805126	-7.226520196	4.311584655	-8.67491836	-4.593954341	4.951890733	2.312698418	5.389295297	4.162710165	-4.120115251	6.460062495	-5.918202	4.378194819	8.283512961	3.296346181	8.276022108	1.029513781	-6.194827379	-7.807857574	-2.676611804	4.432149691	-5.538285553	-6.308218455	-7.987241194	-0.444197541	-3.66546201	9.128210896	-4.699697969	-9.10969659	7.805516282	-3.494860162	-6.453175021	-9.31932478	6.625913394	-1.646546145	7.30859472	9.468024071	6.278041711	-4.27418178	2.48639681	8.944439788	-1.360981512	5.988731845	8.563074116	1.175779182	-7.279412277	6.81322818	-6.731783158	9.976898103	-9.158753914	8.014820589	2.211723108	1.410737146	0.709524186	0.484312737	9.885595525	9.600899919	-2.803431289	-7.359383703	-2.555279181	7.859968658	1.467568422	1.954455074	-3.760343685	-6.109753388	-5.495211346	-2.266978166	3.783399642	6.0329867	4.577173234	3.350947163	-6.927355579	-9.679327877	9.555465985	4.547494066	-5.384039623	-9.637381052	-7.84957323	8.487604601	3.014494898	-2.055674635	-1.316067048	5.706447893	1.344391952	1.921764218	-6.331594159	-4.540522539	-4.50516283	2.525187379	3.796944227	8.769664161	-6.307432362	-2.07928383	5.544398706	9.482688978	-1.172370226	9.766187814	-5.666388957	6.516536142	-3.287286154	6.518840253	-6.588543664	1.705684379	-3.265327582	0.259893231	-9.991945324	9.283972527	-6.233437057	6.86004249	-9.470929839	2.671598148	6.081413589	2.436304809	1.293476201	7.970134049	-2.123885127	5.075405222	6.260584192	-4.003878181	7.274841189	-8.37629123	4.216887641	-3.667209101	8.547108188	8.859942456	5.14715433	-3.52119618	-5.692963494	4.937588268	-8.520482073	0.371981618	-6.055418776	-2.274106985	-5.578252851	7.481728073	-7.427318556	0.62156383	2.190007642	-0.079535029	0.115173504	-9.65877035	0.329262009	-7.491295775	9.707148302	-6.503980465	6.614984361	-4.561655316	-1.889310708	3.606101263	-4.544369322	-5.442923532	7.439554679	-8.06622037	-0.145767363	-4.317011215	5.125190781	-4.687274156	7.938308498	-4.536350936	-3.767074861	-9.690176973	-5.382989678	-9.044614572	4.240026589	-3.63213408	1.108249465	-3.225660506	-2.039241859	1.37105544	4.219804943	4.038958554	9.5910888	3.184248653	-0.820291795	-3.096646795	-7.666151178	-1.749463251	7.143876258	-3.465195812	-6.956073646	2.377289455	-8.656619112	6.6532722	1.975978533	-5.545531254	-9.746862207	9.394230794	6.055374377	4.545520988	-2.017291562	0.21174817	-3.085466412	3.915521792	-1.17441692	3.389331231	4.028731456	-0.164054104	-8.098427091	-2.805864214	-1.883137783	3.583875451	1.358097514	6.589801723	-0.047839573	8.768842206	8.520617181	-6.828993313	9.98166827	-6.40720663	3.687975324	7.261181515	3.716857511	-6.748481636	-5.531959747	-9.012943852	-4.434642785	-0.228051917	2.349772444	-4.677578753	-1.268891122	-0.409067677	1.910149905	-0.046642535	2.799880558	-4.828554673	2.346160053	-9.23966062	-7.628382939	9.601434025	-3.691794071	7.193792137	-6.926376253	9.126729662	8.617862049	-2.398544319	8.47737129	-8.010147901	-9.414701531	-1.718327196	-4.535272229	7.26983749	4.287303229	-1.389502552	5.098737531	-1.116481371	-3.85471942	-8.890453864];

input=readmatrix('2021-2022-2023.xlsx','Sheet','2021-2022-2023','Range','P5:AE105124');
dimension_input=size(input)
longitud=dimension_input(1)
j=1;
for i=1:longitud
    if input(i,dimension_input(2))>0
        input_sin_ceros(j,:)=input(i,:);
        j=j+1;
    end
end

entrada_temp=input_sin_ceros(:,1);
entrada_rad=input_sin_ceros(:,14);
salida=input_sin_ceros(:,16);
x1=[entrada_temp entrada_rad];
y1=salida;

day=readmatrix('2021-2022-2023.xlsx','Sheet','2021-2022-2023','Range','P177989:AE178276');
prueba_temp=day(:,1);
prueba_rad=day(:,14);
prueba=[prueba_temp prueba_rad];
medida=day(:,16);

y2=(y1-min(y1))./(max(y1)-min(y1));
for i=1:2
    x2(:,i)=(x1(:,i)-min(x1(:,i)))/(max(x1(:,i))-min(x1(:,i)));
    prueba_normalizada(:,i)=(prueba(:,i)-min(x1(:,i)))/(max(x1(:,i))-min(x1(:,i)));
end
medida_norm(:,1)=(medida(:,1)-min(y1(:,1)))/(max(y1(:,1))-min(y1(:,1)));

xt=x2';
yt=y2';

Num_max_neuronas=100;
if ceil(abs(x(1)))>100
    hiddenLayerSize=100;
else hiddenLayerSize=ceil(abs(x(1)));
end
hiddenLayerSize 
valor_transferFcn_1=ceil(abs(x(2)));
valor_transferFcn_2=ceil(abs(x(3)));
valor_trainFcn=ceil(abs(x(4)));

if valor_transferFcn_1>15 | valor_transferFcn_2>15 | valor_trainFcn>9
    valor_transferFcn_1=15;
    valor_transferFcn_2=15;
    valor_trainFcn=9;
end

x_h=x(5:length(x));
longitud_x_h=length(x_h)
x_v=x(5:length(x))';

net = feedforwardnet(hiddenLayerSize);

net.divideParam.trainRatio=70/100;
net.divideParam.valRatio=15/100;
net.divideParam.testRatio=15/100;

net = configure(net,xt,yt);
net.layers{1}.size=hiddenLayerSize;
vector=zeros(hiddenLayerSize,2);
p=1;
for i=1:hiddenLayerSize
    for j=1:2
        vector(i,j)=x_h(p);
        p=1+p;
    end
end
net.IW{1}=vector;
net.LW{2}=x_h(p:p+hiddenLayerSize-1);
net.b{1}=x_v(p+hiddenLayerSize:p+hiddenLayerSize*2-1);
net.b{2}=x_h(p+hiddenLayerSize*2);

switch valor_trainFcn
    case 1
        net.trainFcn='trainlm';
    case 2
        net.trainFcn='trainbfg';
    case 3
        net.trainFcn='trainrp';
    case 4
        net.trainFcn='trainscg';
    case 5
        net.trainFcn='traincgb';
    case 6
        net.trainFcn='traincgf';
    case 7
        net.trainFcn='traincgp';
    case 8
        net.trainFcn='trainoss';
    case 9
        net.trainFcn='traingdx';
end
switch valor_transferFcn_1
    case 1
        net.layers{1}.transferFcn='compet';
    case 2
        net.layers{1}.transferFcn='elliotsig';
    case 3
        net.layers{1}.transferFcn='hardlim';
    case 4
        net.layers{1}.transferFcn='hardlims';
    case 5
        net.layers{1}.transferFcn='logsig';
    case 6
        net.layers{1}.transferFcn='netinv';
    case 7
        net.layers{1}.transferFcn='poslin';
    case 8
        net.layers{1}.transferFcn='purelin';
    case 9
        net.layers{1}.transferFcn='radbas';
    case 10
        net.layers{1}.transferFcn='radbasn';
    case 11
        net.layers{1}.transferFcn='satlin';
    case 12
        net.layers{1}.transferFcn='satlins';
    case 13
        net.layers{1}.transferFcn='softmax';
    case 14
        net.layers{1}.transferFcn='tansig';
    case 15
        net.layers{1}.transferFcn='tribas';
end
switch valor_transferFcn_2
    case 1
        net.layers{2}.transferFcn='compet';
    case 2
        net.layers{2}.transferFcn='elliotsig';
    case 3
        net.layers{2}.transferFcn='hardlim';
    case 4
        net.layers{2}.transferFcn='hardlims';
    case 5
        net.layers{2}.transferFcn='logsig';
    case 6
        net.layers{2}.transferFcn='netinv';
    case 7
        net.layers{2}.transferFcn='poslin';
    case 8
        net.layers{2}.transferFcn='purelin';
    case 9
        net.layers{2}.transferFcn='radbas';
    case 10
        net.layers{2}.transferFcn='radbasn';
    case 11
        net.layers{2}.transferFcn='satlin';
    case 12
        net.layers{2}.transferFcn='satlins';
    case 13
        net.layers{2}.transferFcn='softmax';
    case 14
        net.layers{2}.transferFcn='tansig';
    case 15
        net.layers{2}.transferFcn='tribas';
end
net
tic
entrenamiento_yt=(net(xt))';
entrenamiento_yt_desnorm=entrenamiento_yt*(max(y1)-min(y1))+min(y1);
RMSE_train=sqrt((sum((entrenamiento_yt_desnorm-y1).^2))/length(y1))

prueba_output_normalizada=net(prueba_normalizada');
p_pv_sin_corregir=prueba_output_normalizada*(max(y1)-min(y1))+min(y1);

RMSE_p_pv_sin_corregir=sqrt((sum((p_pv_sin_corregir'-medida).^2))/length(medida))
figure()
plotregression(medida,p_pv_sin_corregir,'Regression')
j=1;
p_pv_dia_solo=[];
for i=1:length(p_pv_sin_corregir)
    if prueba_rad(i)==0
        p_pv(i)=0;
    else
        p_pv(i)=p_pv_sin_corregir(i);
        p_pv(i)
        p_pv_dia_solo(j)=p_pv(i);
        medida_dia_solo(j)=medida(i);
        j=j+1;
    end
end
RMSE=sqrt((sum((p_pv'-medida).^2))/length(medida))

nRMSE_machine_quizas_el_que_toca=100*sqrt((sum(((p_pv_dia_solo-medida_dia_solo)/3300).^2))/length(medida_dia_solo))
nRMSE_wnn_quizas_el_que_toca=(1/3300)*sqrt((sum((p_pv_dia_solo-medida_dia_solo).^2))/length(medida_dia_solo))*100

MAE_wnn=(1/length(medida_dia_solo))*sum(abs(p_pv_dia_solo-medida_dia_solo))
MAPE_wnn=(1/length(medida_dia_solo))*sum(abs((p_pv_dia_solo-medida_dia_solo)./medida_dia_solo))*100

medida_dia_solo=medida_dia_solo';
p_pv_dia_solo=p_pv_dia_solo';

prediccion=p_pv;
tiempo_forecast=toc

figure()
plot(medida,'b')
hold on
plot(p_pv,'r')
hold off
legend('medida','predicción(p_pv)')
filename = 'Resultados_predicciones.xlsx';
nombre={'x optimizada (parámteros ANN):'};
writecell(nombre,filename,'Sheet',1,'Range','A1');
writematrix(x,filename,'Sheet',1,'Range','B1');
nombre={'Population:'};
writecell(nombre,filename,'Sheet',1,'Range','A2');
nombre={'Nº nuronas (redondeo al inmediato superior):','TransferFcn_1 (redondeo al inmediato superior):','TransferFcn_2 (redondeo al inmediato superior):','TrainFcn (no uso):'};
writecell(nombre,filename,'Sheet',1,'Range','A3');
p_pv=p_pv';
writematrix(population,filename,'Sheet',1,'Range','A4');
nombre={'RMSE train optimizada:'};
writecell(nombre,filename,'Sheet',2,'Range','A1');
writematrix(fval,filename,'Sheet',2,'Range','B1');
nombre={'Score:'};
writecell(nombre,filename,'Sheet',2,'Range','A2');
writematrix(score,filename,'Sheet',2,'Range','A3');
nombre={'Medida:'};
writecell(nombre,filename,'Sheet',3,'Range','A1');
writematrix(medida,filename,'Sheet',3,'Range','A2');
nombre={'Predicción:'};
writecell(nombre,filename,'Sheet',3,'Range','B1');
writematrix(p_pv',filename,'Sheet',3,'Range','B2');

